//
//  QQLogin.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/11.
//

//
//  QQLogin.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/11.
//

import Foundation
import SwiftUI
import LeanCloud
class QQLogin : LoginProtocol {
    
    let typeKey = LoginType.QQ.rawValue
    
    private var tencentOAuth: TencentOAuth!
    func doLogin() {
        SceneQQDelegate.shared.login()
    }
    
    func bind() {
       
    }
    
    
    func doAuthSuc(code: String) {
        //无需用使用
    }
    
    func doAuthSuc(access_token: String, openid: String) {
        //代理那边处理
    }
    
    func getAuthInfo(code: String) async throws -> (String, String, UserBean) {
        //无需使用
        return ("","", UserBean())
    }
    
    func getAuthInfo(access_token: String, openid: String) async throws -> (String, String, UserBean) {
        //获取第三方登陆配置
        let accountBean = try await retry(maxRetries: 5, retryDelayMillis: 200) {
            let query = LCQuery(className: "ThirdAccount")
            return try await LeancloudManager.instance.getFirst(query: query, modeType: ThirdAccountBean.self)
        }
       
        let qqInfo = try await retry(maxRetries: 5, retryDelayMillis: 200) {
            return try await TencentNetManager.shared.getQQInfo(access_token: access_token, openid: openid, appId: accountBean?.qqId ?? "", modeType: QQUserInfo.self)
        }
        let newUserBean = UserBean()
        newUserBean.name = qqInfo.nickname
        newUserBean.sex = qqInfo.gender_type
        newUserBean.headUrl = qqInfo.figureurl_qq_2
        return (typeKey, openid ,newUserBean)
    }
    
    
    func doLoginAccount(type: String, id: String, newUserBean: UserBean) {
        //在代理那边处理
    }
}
